package com.wyf.test.view

import android.content.Intent
import android.support.v4.app.FragmentActivity
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.util.Log

import com.wyf.test.R
import java.net.ServerSocket
import java.net.Socket
import android.text.TextUtils
import android.widget.Toast
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import java.io.PrintWriter


/**
 * 引导页
 */
class SplishActivity : FragmentActivity() {

    private val TAG = "SplishActivity"

    val SERVER_PORT = 10086

    private var threadFlag = false

    var serverSocket: ServerSocket? = null

    private val handler = object : Handler() {
        override fun handleMessage(msg: Message) {
            when (msg.what) {
                1000 -> Log.v(TAG,"Socket链接成功")
                1001 -> Log.v(TAG,"接收到消息:" + msg.obj.toString())
            }
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splish)
        usbTest()
//        go()
    }

    override fun onDestroy() {
        super.onDestroy()
        threadFlag = false;
    }

    private fun usbTest() {
        if (threadFlag) {
            threadFlag = false
        } else {
            threadFlag = true
            Thread(Runnable {
                Log.i(TAG, "Server启动")
                doSocket()
            }).start()
        }
    }

    private fun doSocket() {
        try {
            serverSocket = ServerSocket(SERVER_PORT)
            while (threadFlag) {
                val socket = serverSocket!!.accept()
                if (socket != null && socket.isConnected) {
                    handler.sendEmptyMessage(1000)
                    Toast.makeText(this@SplishActivity, "链接成功", Toast.LENGTH_SHORT).show()
                    Log.i(TAG, "链接客户端IP：" + socket.inetAddress.hostAddress)
                    Thread(SocketServer(socket)).start()
                }
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }

    }

    private inner class SocketServer(private val socket: Socket?) : Runnable {

        override fun run() {
            Log.i(TAG, "发送消息")
            var bis: BufferedReader? = null
            var bos: PrintWriter? = null
            try {
                bis = BufferedReader(InputStreamReader(socket!!.getInputStream(), "utf-8"))
                bos = PrintWriter(socket!!.getOutputStream(), true)
                bos!!.println("success")
                //                bos.flush();
                while (threadFlag) {
                    val msg = bis!!.readLine()
                    if (!TextUtils.isEmpty(msg)) {
                        val message = Message()
                        message.what = 1001
                        message.obj = msg
                        handler.sendMessage(message)
                        Log.i(TAG, "接收到：" + msg)
                        bos!!.println("接收到了客户端消息。")
                        //                        bos.flush();
                    }
                }
            } catch (e: IOException) {
                e.printStackTrace()
            } finally {
                try {
                    if (bos != null) {
                        bos!!.flush()
                        bos!!.close()
                    }
                    if (bis != null) {
                        bis!!.close()
                    }
                    if (socket != null) {
                        socket!!.close()
                    }
                } catch (e: IOException) {
                    e.printStackTrace()
                }

            }
        }
    }

    private fun go() {
        Thread(Runnable {
            try {
                Thread.sleep(1000)
            } catch (e: InterruptedException) {
                e.printStackTrace()
            }

            startActivity(Intent(this@SplishActivity, WebTestActivity::class.java))
            finish()
        }).start()
    }

}
